﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Employee_CreateInterview : System.Web.UI.Page
{
    EmployeeMethord EM = new EmployeeMethord();
    protected void Page_Load(object sender, EventArgs e)
    {
        txtEmployeeCreate.Text = EM.ReturnNameEmployee("H0001");
        txtDateInterview_CalendarExtender.StartDate = DateTime.Today;
        AddItemsVacancy();
    }
    private void AddItemsVacancy()
    {
        try
        {
            if (ddltitlevacancy.Items.Count <= 1)
            {
                SqlDataReader dr = EM.ReturnVacancyWithUserName("H0001");
                ddltitlevacancy.DataSource = dr;
                ddltitlevacancy.DataTextField = "Title";
                ddltitlevacancy.DataValueField = "VacancyID";
                ddltitlevacancy.DataBind();
                EM.connClose();
                ddltitlevacancy.Items.Insert(0, "Please choise title vacancy");
            }
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
        }
    }
    private void AddItemsApplicant()
    {
        try
        {
            SqlDataReader dr = EM.ReturnApplicantWithVacancy(ddltitlevacancy.SelectedValue);
            ddlApplicant.DataSource = dr;
            ddlApplicant.DataTextField = "FullName";
            ddlApplicant.DataValueField = "ApplicantID";
            ddlApplicant.DataBind();
            EM.connClose();
            ddlApplicant.Items.Insert(0, "Please choise Applicant");
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
        }
    }
    private void AddItemsInterviewer()
    {
        try
        {
            SqlDataReader dr = EM.ReturnInterviewer();
            ddlInterviewer.DataSource = dr;
            ddlInterviewer.DataTextField = "FullName";
            ddlInterviewer.DataValueField = "InterViewerID";
            ddlInterviewer.DataBind();
            EM.connClose();
            ddlInterviewer.Items.Insert(0, "Please choise Interviewer");
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
        }
    }
    protected void btnCreate_Click(object sender, EventArgs e)
    {
        lblSuccess.Text = "";
        if (ddltitlevacancy.SelectedValue.Equals("Please choise title vacancy"))
        {
            lblErrortitlevacancy.Text = "(*)";
            lblError.Text = "Please choise value for title vacancy.";
        }
        else
        {
            lblErrortitlevacancy.Text = "";
            if (txtDateInterview.Text.Equals(""))
            {
                lblErrorDateInterview.Text = "(*)";
                lblError.Text = "Date Interview can't be empty.";
            }

            else
            {
                lblErrorDateInterview.Text = "";
                if (ddlApplicant.SelectedValue.Equals("Please choise Applicant"))
                {
                    lblErrorApplicant.Text = "(*)";
                    lblError.Text = "Please choise value for Applicant Name.";
                }
                else
                {
                    lblErrorApplicant.Text = "";
                    if (ddlInterviewer.SelectedValue.Equals("Please choise Interviewer"))
                    {
                        lblErrorInterviewer.Text = "(*)";
                        lblError.Text = "Please choise value for Interviewer.";
                    }
                    else
                    {
                        lblErrorInterviewer.Text = "";
                        if (txtTimeStart.Text.Equals(""))
                        {
                            lblErrorTime.Text = "(*)";
                            lblError.Text = "Time Details can't be empty.";
                        }
                        else
                        {
                            lblErrorTime.Text = "";
                            lblError.Text = "";
                            try
                            {
                                DateTime dateinterview = Convert.ToDateTime(txtDateInterview.Text.ToString());
                                bool result = EM.InsertIntoTableInterview(ddlInterviewer.SelectedValue, ddltitlevacancy.SelectedValue, ddlApplicant.SelectedValue, dateinterview, txtTimeStart.Text);
                                if (result)
                                {
                                    lblSuccess.Text = "Add interview success.";
                                }
                                else
                                {
                                    lblError.Text = "Add interview faild.";
                                }
                            }
                            catch (Exception ex)
                            {
                                lblErrorDateInterview.Text = "(*)";
                                lblError.Text = "Check Date interview";
                                Console.Write(ex.Message);
                            }
                        }
                    }
                }
            }
        }
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        ddltitlevacancy.ClearSelection();
        ddlApplicant.ClearSelection();
        ddlInterviewer.ClearSelection();
        txtDateInterview.Text = "";
        txtTimeStart.Text = "";
        lblError.Text = "";
        lblErrorApplicant.Text = "";
        lblErrorDateInterview.Text = "";
        lblErrorInterviewer.Text = "";
        lblErrorTime.Text = "";
        lblErrortitlevacancy.Text = "";
        lblSuccess.Text = "";
    }
    protected void ddltitlevacancy_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddltitlevacancy.SelectedIndex != 0)
        {
            for (int i = 0; i < ddlApplicant.Items.Count; i++)
            {
                ddlApplicant.Items.RemoveAt(i);
            }
            for (int i = 0; i < ddlInterviewer.Items.Count; i++)
            {
                ddlInterviewer.Items.RemoveAt(i);
            }
            AddItemsApplicant();
            AddItemsInterviewer();
        }
        else
        {
            ddlApplicant.Items.Clear();
            ddlInterviewer.Items.Clear();
            ddlApplicant.Items.Insert(0, "Please choise Applicant");
            ddlInterviewer.Items.Insert(0, "Please choise Interviewer");
        }
    }
}